package com.wxy.ej.flow.modules.act.listen;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 子流程调用了4次（4个机构）
 * 1001
 * [1001, 1002, 1003, 1004]
 */
@Slf4j
@Component("subProcessListener")
@Scope("prototype")
public class SubProcessListener implements ExecutionListener {


    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("---->act listener: SubProcessListener");
        System.out.println("=== SubProcessListener ====");
        System.out.println("EventName:" + delegateExecution.getEventName());
        System.out.println("ProcessDefinitionId:" + delegateExecution.getProcessDefinitionId());
        System.out.println("ProcessInstanceId:" + delegateExecution.getProcessInstanceId());
        //String companyId = (String)delegateExecution.getVariable("companyId");
        Map company = (Map)delegateExecution.getVariable("company");
        Long cmpyId = (Long)company.get("cmpyId");
        String cmpyName = (String)company.get("cmpyName");
/*

        List<Long> companyIdList = new ArrayList<>();
        List<String> companyNameList = new ArrayList<>();

        List<Map> companyList = (List<Map>)delegateExecution.getVariable("companyList");
        List<Map<String, String>> currentUserAllList = new ArrayList<>();
        Map currentCompanyMap = new HashMap();
        for (Map _companyMap : companyList) {
            Long _companyId = (Long) _companyMap.get("cmpyId");
            String _companyName = (String) _companyMap.get("cmpyName");
            String taskContent4Msg = (String)_companyMap.get("taskContent4Msg");
            List<Map<String, String>> userMapList = (List)_companyMap.get("userMapList");  // [{userId:'', userName:''}]
            companyIdList.add(_companyId);
            companyNameList.add(_companyName);

            if (_companyId.longValue() == cmpyId.longValue()) {
                currentUserAllList = userMapList;
                currentCompanyMap = _companyMap;

            }
        }
        // 系统用户, userId>0
        List<Map<String, String>> userList = new ArrayList<>();
        // 通知用户, userId<=0
        List<Map<String, String>> userNoticeList = new ArrayList<>();

        // 所有用户姓名，做日志用
        List<String> userNameList = new ArrayList<>();
        List<String> userNameNoticeList = new ArrayList<>();
        for (Map<String,String> _userMap : currentUserAllList) {
            String userId = _userMap.get("userId");
            String phone = _userMap.get("phone");
            // 只发通知的用户
            if (!StringUtils.isNull(phone)) {
                userNoticeList.add(_userMap);
                userNameNoticeList.add(_userMap.get("userName"));
            }
            // 系统用户
            if (!StringUtils.isNull(userId) && Integer.parseInt(userId)>0){
                userList.add(_userMap);
                userNameList.add(_userMap.get("userName"));
            }
        }

        delegateExecution.setVariableLocal("companyMap", currentCompanyMap);
        delegateExecution.setVariableLocal("userList", userList);
        delegateExecution.setVariableLocal("userNoticeList", userNoticeList);

        // 流程执行日志
        FlowElement flowElement = delegateExecution.getCurrentFlowElement();
        // ${companyId}
        String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateExecution);

        CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
        taskLogDto.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
        taskLogDto.setFlowElementId(flowElement.getId());
        taskLogDto.setFlowElementName(flowElement.getName());
        taskLogDto.setFlowElementDoc(doc);
        taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
        StringBuilder sb = new StringBuilder();
        sb.append("子流程共有："+companyNameList).append("; ");
        sb.append("当前子流程："+cmpyName).append("; ");
        sb.append("当前子流程系统用户："+userNameList).append("; ");
        sb.append("当前子流程通知用户："+userNameNoticeList).append("; ");
        taskLogDto.setDescription(sb.toString());
        ctcTaskLogBizService.create(taskLogDto);
*/

    }


}
